let curplace, actyinfo, placeId;
const request = require("../../utils/request.js").request;
const url = require("../../utils/request.js").commonUrl;
const app = getApp();
var op;
// pages/place.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        url: url,
        imgUrls: [],
        title: "",
        acties: "",
        brief: "",
        nearList: [],
        id: ""
    },
    //跳转到活动详情页
    detailGo: function (event) {
        let index = event.currentTarget.dataset.itemid;
        let activityId = this.data.acties[index].activityId;
        let activityAreaId = this.data.acties[index].activityAreaId;
        var detailInfo = {
            activityId: activityId,
            activityAreaId: activityAreaId
        }

        wx.navigateTo({
            url: '../details/details?detailInfo=' + JSON.stringify(detailInfo),
            success: function (res) {
            },
            fail: function (res) {
            },
            complete: function (res) {
            },
        })
    },
    //跳转到活动点
    place: function (e) {
        var id = e.currentTarget.dataset.index;
        var cur = this.data.nearList[id];
        actyinfo = {
            id: cur.id,
            actId: cur.activityAreaId,
            curlati: cur.latitude,
            curlongi: cur.longitude
        }
        wx.redirectTo({
            url: '../place/place?act=' + JSON.stringify(actyinfo)
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        op = JSON.parse(options.act);
        placeId = op.actId;
        curplace = {
            latitude: op.curlati,
            longitude: op.curlongi
        }
        this.setData({
            id: op.id
        })
        this.getNear();
        this.getPlaceInfo();
    },
    getPlaceInfo: function () {
        var that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        var params = {
            activityAreaId: placeId,
            weixin: app.globalData.openid
        }
        request("m/activityarea/get.json", params).then(function (data) {
            wx.hideLoading();
            var res = data.data.result;
            //console.log("place",data)
            var imgs = [];
            for (var i = 0; i < res.uploadFileVos.length; i++) {
                imgs.push(url + "/upload/get/image.json?url=" + res.uploadFileVos[i].fileUrl)
            }
            that.setData({
                title: res.areaName,
                brief: res.brief,
                acties: res.activityVos,
                imgUrls: imgs
            })
        }).catch(function (err) {
            console.log(err)
        })
    },
    getNear: function () {

        var that = this;
        var params = {
            weixin: app.globalData.openid,
            leftCorner: (curplace.longitude - 0.5) + "," + (curplace.latitude + 0.5),
            rightCorner: (curplace.longitude + 0.5) + "," + (curplace.latitude - 0.5),
        }
        request("m/activityarea/list.json", params).then(function (data) {
            var re = [];

            for (var i = 0; i < data.data.result.length; i++) {
                if (i != that.data.id) {
                    re.push(data.data.result[i])
                }
            }
            that.setData({
                nearList: re
            })
            //console.log(re)

        }).catch(function (err) {
            console.log(err)
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            path:"pages/place/place?act=" + JSON.stringify(op)
        }
    }
})